

#include <material/programeditor/programtreemodel.h>
#include <qmimedata.h>

cceMaterialProgramTreeModel::cceMaterialProgramTreeModel (cceMaterialManager* mgr)
	: QAbstractItemModel ()
	, _mgr (mgr)
{
	connect (_mgr, SIGNAL(ProgramAdded (cceMaterialProgram*)), this, SLOT(ResetContent ()));
	connect (_mgr, SIGNAL(ProgramChanged (cceMaterialProgram*)), this, SLOT(ResetContent ()));
	connect (_mgr, SIGNAL(ProgramRemoved (cceMaterialProgram*)), this, SLOT(ResetContent ()));

	setSupportedDragActions (Qt::ActionMask);
}

void cceMaterialProgramTreeModel::ResetContent ()
{
	reset ();
}

int cceMaterialProgramTreeModel::columnCount (const QModelIndex&) const
{
	return Column_COUNT;
}

QVariant cceMaterialProgramTreeModel::data (const QModelIndex& index, int role) const
{
	if (!index.isValid ())
		{
			return QVariant ();
		}

	cceMaterialProgram* program = static_cast<cceMaterialProgram*>(index.internalPointer ());

	if (role == Qt::DisplayRole)
		{
			switch (index.column ())
				{
				case Column_Name:
					return QVariant (program->GetName ());
				default:
					break;
				}
		}

	return QVariant ();
}

QModelIndex cceMaterialProgramTreeModel::index (int row, int column, const QModelIndex &parent) const
{
	if (parent.isValid ()) return QModelIndex ();
	if (column < 0 || column >= Column_COUNT) return QModelIndex ();
	if (row < 0 || row >= _mgr->GetNumberOfPrograms ()) return QModelIndex ();
	return createIndex (row, column, _mgr->GetProgram (row));
}

QModelIndex cceMaterialProgramTreeModel::parent (const QModelIndex &index) const
{
	return QModelIndex ();
}

int cceMaterialProgramTreeModel::rowCount (const QModelIndex &parent) const
{
	if (parent.isValid ()) return 0;
	return _mgr->GetNumberOfPrograms ();
}

Qt::ItemFlags cceMaterialProgramTreeModel::flags (const QModelIndex& index) const
{
	Qt::ItemFlags f = QAbstractItemModel::flags (index);
	if (index.isValid()) 
		{
			f |= Qt::ItemIsDragEnabled;
		}
	return f;
}

QString cceMaterialProgramTreeModel::GetProgramName (unsigned row) const
{
	if (row < 0 || row >= _mgr->GetNumberOfPrograms ()) return QString::Null ();
	cceMaterialProgram* program = _mgr->GetProgram (row);
	if (!program) return QString::Null ();
	return program->GetName ();
}


QMimeData* cceMaterialProgramTreeModel::mimeData (const QModelIndexList& indices) const
{
  if (indices.size () == 0)
    {
      return 0;
    }
  const QModelIndex& index = indices[0];
  if (!index.isValid ())
    {
    }

  int row = index.row ();
  foreach (const QModelIndex& idx, indices)
    {
      if (!idx.isValid () || idx.row () != row)
        {
          return 0;
        }
    }

  cceMaterialProgram* program = static_cast<cceMaterialProgram*>(index.internalPointer ());
  if (!program)
    {
      return 0;
    }

  QMimeData* mimeData = new QMimeData ();
  mimeData->setData (CCE_MATERIAL_PROGRAMNAME_MIME, program->GetName ().toAscii ());
  return mimeData;
}

		
QVariant cceMaterialProgramTreeModel::headerData (int section, Qt::Orientation orientation, int role) const
{
	if (section < 0 || section >= Column_COUNT || role != Qt::DisplayRole)
		{
			return QVariant ();
		}
	Q_UNUSED (orientation);
	switch (section)
		{
		case Column_Name:
			return "Name";
		default:
			break;
		}
	return QVariant ();
}

